package com.aduerr.v1shouyin.mvp.widget;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.mvp.fragment.CancelFFragment;
import com.aduerr.v1shouyin.mvp.fragment.CancelSFragment;
import com.aduerr.v1shouyin.mvp.fragment.CancelTFragment;
import com.aduerr.v1shouyin.util.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HI
 * @email dev1024@foxmail.com
 * @time 2018/10/23 13:39
 * @change
 * @chang time
 * @class describe
 */

public class CancelDialog extends DialogFragment {

    Unbinder unbinder;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;

    List<Fragment> mFragmentList = new ArrayList<>();
    CancelFFragment cancelFFragment;
    CancelSFragment cancelSFragment;
    CancelTFragment cancelTFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.dialog);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.dialog_fragment, container);
        unbinder = ButterKnife.bind(this, mView);
        cancelFFragment = CancelFFragment.newInstance("0");
        cancelSFragment = CancelSFragment.newInstance("1");
        cancelTFragment = CancelTFragment.newInstance("2");


        if(mFragmentList.size() == 0) {
            mFragmentList.add(cancelFFragment);
            mFragmentList.add(cancelSFragment);
            mFragmentList.add(cancelTFragment);
        }

        CancleDialogAdapter cancleDialogAdapter =
                new CancleDialogAdapter(getChildFragmentManager(), mFragmentList);
        mViewPager.setAdapter(cancleDialogAdapter);
        getDialog().setCanceledOnTouchOutside(false);
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return mView;
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        try {
            //在每个add事务前增加一个remove事务，防止连续的add
            manager.beginTransaction().remove(this).commit();
            super.show(manager, tag);
        } catch(Exception e) {
            //同一实例使用不同的tag会异常,这里捕获一下
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getDialog().getWindow().setLayout(Tools.Dp2Px(getContext(), 280),
                Tools.Dp2Px(getContext(), 400));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    public class CancleDialogAdapter extends FragmentPagerAdapter {
        private List<Fragment> mFragmentList;

        public CancleDialogAdapter(FragmentManager fm, List<Fragment> mFragmentList) {
            super(fm);
            this.mFragmentList = mFragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return this.mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return this.mFragmentList.size();
        }
    }
}
